home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / comm / bbs / cit_src_AD08.lha / sysarc.c < prev    next >
C/C++ Source or Header  |  1997-07-27  |  11KB  |  429 lines

  1. /*
  2. *       sysarc.c
  3. *
  4. * This file contains the system dependent code for deARCing files for
  5. * download.
  6. */
  7. /*
  8. *       history
  9. *
  10. * 91Oct07 HAW Netcache stuff.
  11. * 88Jun08 HAW Created.
  12. */
  13. #define SYSTEM_DEPENDENT
  14. #include "ctdl.h"
  15. #include "dos.h"
  16.  
  17. #define EXTERN extern
  18. #include "global_data.h"
  19.  
  20. /*#include "sys\stat.h" */
  21. /* #include "process.h" */
  22. /*
  23.  
  24. *       Contents
  25. *
  26. * ArcInit()   initialize for deARCing
  27. * CompAvailable()   is compression available for this type?
  28. * SendArcFiles()    get arc name, send contents specified
  29. * # MakeTempDir()   Create and move into a temporary dir
  30. *
  31. *   # == local for this implementation only
  32. */
  33.  
  34. extern char logNetResults;
  35. extern char netDebug;
  36. extern FILE  *netLog;
  37.  
  38. extern aRoom roomBuf;
  39. extern MessageBuffer msgBuf;
  40. extern CONFIG  cfg;
  41. extern char whichIO;
  42.  
  43. static char cmdbuf[120];  /* command line buffer */
  44. void KillDeCompress(void);
  45.  
  46.  
  47. /*
  48. * This table must be kept in sync with Formats[] in Misc.c
  49. */
  50. DeCompElement DeComp[] =
  51.   {
  52.     { "delha.sys", "L. Lha",   NULL, NULL, NULL },
  53.     { "dezip.sys", "P. Zip",   NULL, NULL, NULL },
  54.     { "dezoo.sys", "Z. Zoo",   NULL, NULL, NULL },
  55.     { "dearc.sys", "A. Arc",   NULL, NULL, NULL },
  56.  
  57.   };
  58.  
  59. int Jsystem(char *);
  60. static void CheckCompressedData(char *name, int index);
  61. static void MakeTempName(void);
  62. /*
  63. * ArcInit()
  64. *
  65. * This initializes this module for handling the various variants of
  66. * compression/decompression.
  67. */
  68. void ArcInit()
  69.   {
  70.   SYS_FILE name;
  71.   int i;
  72.   for (i = 0; i < NumElems(DeComp); i++)
  73.       {
  74.     makeSysName(name, DeComp[i].FileName, &cfg.roomArea);
  75.     CheckCompressedData(name, i);
  76.  
  77.     }
  78.  
  79.   }
  80. /*
  81. * CheckCompressedData()
  82. *
  83. * This function does an initial check for existence of a configuration file.
  84. */
  85. static void CheckCompressedData(char *name, int index)
  86.   {
  87.   FILE *fd;
  88.   char work[80];
  89.   if (cfg.BoolFlags.debug) splitF(NULL,"Open Attemp:%s\n",name);
  90.   if ((fd = fopen(name, "r")) == NULL)
  91.       {
  92.     return;
  93.  
  94.     }
  95.   if (cfg.BoolFlags.debug) splitF(NULL,"Opened:%s\n",name);
  96.   if (GetAString(work, sizeof work, fd) != NULL)
  97.       {
  98.       if (cfg.BoolFlags.debug) splitF(NULL,"Read 1:%s\n",work);
  99.       if (strlen(work) != 0)DeComp[index].DeWork = strdup(work);
  100.       if (GetAString(work, sizeof work, fd) != NULL)
  101.         {
  102.         if (cfg.BoolFlags.debug) splitF(NULL,"Read 2:%s\n",work);
  103.         if (strlen(work) != 0) DeComp[index].IntWork = strdup(work);
  104.         if (GetAString(work, sizeof work, fd) != NULL)
  105.           {
  106.           if (cfg.BoolFlags.debug) splitF(NULL,"Read 3:%s\n",work);
  107.           if (strlen(work) != 0) DeComp[index].CompWork = strdup(work);
  108.           };
  109.         };
  110.  
  111.       };
  112.   fclose(fd);
  113.  
  114.   }
  115. /*
  116. * SendArcFiles()
  117. *
  118. * This function gets arc filename, send specified contents.
  119. */
  120. void SendArcFiles(int protocol)
  121.   {
  122.   SYS_FILE name;
  123.   char     *temp;
  124.   int  NumFiles, format;
  125.   extern long netBytes;
  126.   getNormStr("ARCHFN", name, SIZE_SYS_FILE, 0);
  127.   if (strLen(name) == 0) return;
  128.   if (!setSpace(&roomBuf)) return;
  129.   temp = getcwd(NULL, 100);
  130.   sPrintf(msgBuf.mbtext, "%s/%s", temp, name);
  131.   homeSpace();
  132.   if (access(msgBuf.mbtext, 4) == -1)
  133.     {
  134.     for (NumFiles = 0; NumFiles < NumElems(DeComp); NumFiles++)
  135.       {
  136.       sPrintf(msgBuf.mbtext, "%s/%s.%s", temp,name, Formats[NumFiles].Format);
  137.       if (access(msgBuf.mbtext, 4) == 0) break;
  138.  
  139.       };
  140.  
  141.     };
  142.   if( access(msgBuf.mbtext,4) == -1 )
  143.     {
  144.     mPrintf("%s does not exist.\n ", name);
  145.     free(temp);
  146.     return;
  147.  
  148.     };
  149.   if ((format = CompressType(msgBuf.mbtext)) == -1)
  150.   format = 0;
  151.   if (DeComp[format].DeWork == NULL)
  152.       {
  153.     mPrintf("Sorry, de%sing not supported here.\n ",
  154.     Formats[format].Format);
  155.     return;
  156.  
  157.     }
  158.   sPrintf(msgBuf.mbtext, "%s %s/%s ",
  159.   DeComp[format].DeWork, temp, name);
  160.   getNormStr("ARCHMS", msgBuf.mbtext, 100, 0);
  161.   if (strLen(msgBuf.mbtext) == 0) strCpy(msgBuf.mbtext, ALL_FILES);
  162.   MakeTempDir();      /* Create and drop into */
  163.   mPrintf("One moment, please ...\n ");
  164.   sprintf(cmdbuf, "%s %s/%s %s",
  165.   DeComp[format].DeWork, temp, name, msgBuf.mbtext);
  166.   Jsystem(cmdbuf);
  167.   free(temp);         /* Don't need this any longer, so kill it now */
  168.   netBytes = 0l;
  169.   NumFiles = wildCard(getSize, ALL_FILES, FALSE, "", FALSE);
  170.   if (NumFiles <= 0)
  171.       {
  172.     mPrintf("Sorry, no files matched that deARC command.\n ");
  173.  
  174.     }
  175.   else
  176.       {
  177.     if ( getYesNo("MREFFI") )
  178.     if (TranAdmin(protocol, NumFiles))
  179.     TranSend(protocol, transmitFile, ALL_FILES, "", FALSE);
  180.     }
  181.   KillDeCompress();  /* clean up the temp directory */
  182.   }
  183.  
  184. /*
  185.  * KillDeCompress()
  186.  *
  187.  * This function clears out the files from the decompression.
  188.  * We assume we're IN the dir to be cleared.
  189.  */
  190. void KillDeCompress()
  191.   {
  192.   char temp[120];
  193.   homeSpace();
  194.   chdir(TDirBuffer);
  195.   getcwd(temp, 120);
  196.   if( strcmp(temp,TDirBuffer) == 0 )   /* make sure we are where we want to delete files */
  197.     {
  198.     if (!cfg.BoolFlags.debug) wildCard(DelFile, ALL_FILES, FALSE, "", FALSE);
  199.     }
  200.   else
  201.     {
  202.     splitF(netLog,"Error:  Could not set directory and delete temp dir/files\n");
  203.     splitF(netLog,"Temp dir:%s\n",TDirBuffer);
  204.     splitF(netLog,"Curr dir:%s\n",temp);
  205.     }
  206.   homeSpace();
  207.   if (!cfg.BoolFlags.debug) rmdir(TDirBuffer);
  208.   }
  209. /*
  210.  * KillNetDeCompress()
  211.  *
  212.  * This function clears out the files from the net session decompression.
  213.  *
  214.  */
  215. void KillNetDeCompress()
  216.   {
  217.   char temp[120], temp2[120];
  218.   homeSpace();
  219.   MakeNetCache(temp2);
  220.   chdir(temp2);
  221.   getcwd(temp, 120);
  222.   if( strcmp(temp,temp2) == 0 )   /* make sure we are where we want to delete files */
  223.     {
  224.     if (!cfg.BoolFlags.debug) wildCard(DelFile, ALL_FILES, FALSE, "", FALSE);
  225.     }
  226.   else
  227.     {
  228.     splitF(netLog,"Error:  Could not set current directory and delete temp files\n");
  229.     splitF(netLog,"Temporary dir:%s\n",temp2);
  230.     splitF(netLog,"  Current dir:%s\n",temp);
  231.     }
  232.   homeSpace();
  233.   }
  234.  
  235.  
  236.  
  237. /*
  238. * MakeTempDir()
  239. *
  240. * This function will create and move into a temporary directory.
  241. * This will always be off the homearea.
  242. */
  243. void MakeTempDir()
  244.   {
  245.   homeSpace();
  246.   MakeTempName();
  247.   if (mkdir(TDirBuffer) != 0) mPrintf("System error with mkdir!\n ");
  248.   chdir(TDirBuffer);
  249.   getcwd(TDirBuffer, 120);
  250.  
  251.   }
  252. /*
  253. * MakeTempName()
  254. *
  255. * This function gets a temporary name.
  256. */
  257. static void MakeTempName()
  258.   {
  259.   static char *seed = "tmp_%04d";
  260.   int i = 0;
  261.   sPrintf(TDirBuffer, seed, i++);
  262.   while (access(TDirBuffer, 0) != -1) sPrintf(TDirBuffer, seed, i++);
  263.   }
  264.  
  265. #define BAT_FILE  "%s\nif warn \n echo \"1\" >%s\nelse\n echo \"0\" >%s\nendif\n"
  266.  
  267. /*
  268. * FileIntegrity()
  269. *
  270. * This function does a file integrity check.
  271. * Note: we don't use spawn() since it adds 3K to the .EXE.
  272. */
  273. char FileIntegrity(char *filename)
  274.   {
  275.   char *c, *d, check[90], bad, NameUsed = FALSE;
  276.   int format;
  277.   FILE *fd;
  278.   if ((c = strchr(filename, '.')) != NULL)
  279.       {
  280.     if ((format = CompressType(filename)) == -1) return TRUE;
  281.     if (format > COMP_MAX - 1)                   return TRUE;
  282.     if (DeComp[format].IntWork == NULL)          return TRUE;
  283.     if (!getYesNo("INTEGR"))return TRUE;
  284.     Output_Citadel_Message("ONEMOM",NULL, NULL, NULL);
  285.     MakeTempName();
  286.     strCat(TDirBuffer, ".bat");
  287.     if (cfg.BoolFlags.debug) splitF(NULL,"File Open:%s\n",TDirBuffer);
  288.     fd = fopen(TDirBuffer, "w");
  289.     sprintf(check,"Execute %s",TDirBuffer);
  290.     MakeTempName();
  291.     /*
  292.     * Manually copy.  If we encounter "%g" then replace it with the
  293.     * name of the file to test.  If we never encounter it, then
  294.     * remember to append it to the end of the string after the copy
  295.     * has finished (NameUsed).
  296.     */
  297.     for (c = DeComp[format].IntWork, d = msgBuf.mbtext; *c; c++)
  298.         {
  299.       if (*c == '%' && *(c + 1) == 'g')
  300.           {
  301.         strCpy(d, filename);
  302.         NameUsed = TRUE;
  303.         while (*d) d++;
  304.         c++;      /* this will get us over the %g. */
  305.  
  306.         }
  307.       else *d++ = *c;
  308.  
  309.       }
  310.     *d++ = ' ';   /* harmless space padding. */
  311.     *d = 0;
  312.     if (!NameUsed) strCat(msgBuf.mbtext, filename);
  313.     fprintf(fd, BAT_FILE, msgBuf.mbtext, TDirBuffer, TDirBuffer);
  314.     fclose(fd);
  315.     Jsystem(check);
  316.     unlink(check);
  317.     if (cfg.BoolFlags.debug) splitF(NULL,"File Open:%s\n",TDirBuffer);
  318.     bad = TRUE;
  319.     if ((fd = fopen(TDirBuffer, "r")) != NULL)
  320.       {
  321.       if (fgets(check, sizeof check, fd) != NULL) bad = *check != '0';
  322.       fclose(fd);
  323.       unlink(TDirBuffer);
  324.       };
  325.     if (bad) return (char)!getYesNo("FAILIC");
  326.     else Output_Citadel_Message("PASSED",NULL, NULL, NULL);
  327.  
  328.     }
  329.   return TRUE;
  330.  
  331.   }
  332. /*
  333. * CompAvailable()
  334. *
  335. * This function determines if the given compression type has a compression
  336. * method defined.
  337. */
  338. char CompAvailable(char CompType)
  339.   {
  340.   return (char)(DeComp[CompType - 1].CompWork != NULL);
  341.  
  342.   }
  343. /*
  344. * NetDeCompress()
  345. *
  346. * This decompresses files for network dissection.
  347. */
  348. void NetDeCompress(char CompType, SYS_FILE fn)
  349.   {
  350.   char *c;
  351.   SYS_FILE dir;
  352.   char pwd[100];
  353.   if(  netDebug )splitF(netLog, "ComptType:%d File:%s\n", (int)CompType, fn);
  354.   if (DeComp[CompType - 1].DeWork == NULL)
  355.     {
  356.     splitF(netLog," No method for decompression, NetDeCompress aborted\n");
  357.     return;
  358.     };
  359.   strCpy(dir, fn);
  360.   c = &dir[strlen(dir) ];  /* get the directory path */
  361.   while(  *c != '/'  && *c != ':' )
  362.     {
  363.     c--;    /* move to the ':' or '/' */
  364.     };
  365.   c++;
  366.   *c = '\0';               /* terminate the directory spec */
  367.   c  = &fn[strlen(dir)];   /* get filename alone */
  368.   chdir(dir);              /* set the default directory    */
  369.   getcwd(pwd, 100);        /* get the absolute path */
  370.   if(  netDebug )splitF(netLog, "Compressed file dir:%s\n", pwd);
  371.   sPrintf(lbyte(pwd), pwd[strlen(pwd) - 1] == '/' ? "%s" : "/%s", c);
  372.   sprintf(cmdbuf,"%s \"%s\"", DeComp[CompType - 1].DeWork, pwd);
  373.   Jsystem(cmdbuf);         /* and unpack the archive */
  374.   }
  375. /*
  376. * Compress()
  377. *
  378. * This compresses the given files into the given archive file using the
  379. * given file.
  380. */
  381. void Compress(char CompType, char *Files, char *ArcFileName)
  382.   {
  383.   if (!CompAvailable(CompType)) return;
  384.   sprintf(cmdbuf, "%s %s %s", DeComp[CompType - 1].CompWork, ArcFileName, Files);
  385.   Jsystem(cmdbuf);
  386.  
  387.   }
  388. /*
  389. * GetUserCompression()
  390. *
  391. * This function handles a request for a compression protocol.  This can be
  392. * called either for a sysop (when selecting for Mass Transfers) or by a user
  393. * (eventually) for .RC.
  394. *
  395. * This returns the protocol selected (LHA_COMP, etc) or NO_COMP if none
  396. * was selected or if none are available.
  397. */
  398. int GetUserCompression()
  399.   {
  400.   int  rover, count = 0, x;
  401.   MenuId   id;
  402.   char *CompOpts[] =
  403.       {
  404.     " ", " ", " ", " ", ""
  405.     };
  406.     for (rover = 0; rover < NumElems(DeComp); rover++) {
  407.     if (DeComp[rover].CompWork != NULL) {
  408.     count++;
  409.     ExtraOption(CompOpts, DeComp[rover].MenuEntry);
  410.     if (whichIO == MODEM) mPrintf("%s\n ", DeComp[rover].MenuEntry);
  411.     }
  412.     }
  413.     if (count == 0) {
  414.     SysopError(NO_MENU, "No Compression methods");
  415.     return NO_COMP;
  416.     }
  417.     id = RegisterSysopMenu("Compress.mnu", CompOpts, " Compression ");
  418.     SysopMenuPrompt(id, "\n Compression method: ");
  419.     x = GetSysopMenuChar(id);
  420.     CloseSysopMenu(id);
  421.     switch (x) {
  422.     case 'P': return ZIP_COMP;
  423.     case 'Z': return ZOO_COMP;
  424.     case 'L': return LHA_COMP;
  425.     case 'A': return ARC_COMP;
  426.     default:  return NO_COMP;
  427.     }
  428.     }
  429.